var prefix = jsctx + "/rcOperatorAccountRecord";

var operationType = (function () {
    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function reLoad() {
        $(".fixed-table-header-columns").css("display", "none");
        $(".fixed-table-body-columns").css("display", "none");
        $('#exampleTable').bootstrapTable('refresh');
        load();
    };

    function reset() {
        //$("#recordType option:first").prop("selected", 'selected');
        $("#recordType").val('');
        $("#orderNum").val("");
        $("#startTime").val("");
        $("#endTime").val("");
    }

    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    var h = $(window).height() - 80;
    function load() {
        $('#exampleTable')
            .bootstrapTable({
                method: 'get',
                url: prefix + "/listRcOperatorAccountRecord",
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: false,
                dataType: "json",
                pagination: true,
                singleSelect: true,
                pageList: [10, 20, 30],
                pageSize: 10,
                pageNumber: 1,
                sidePagination: "server",
                cache: false,
                height: h,
                paginationShowPageGo: true,
                rightFixedColumns: false,
                showColumns: true,
                queryParams: function (params) {
                    return {
                        limit: params.limit,
                        offset: (params.offset / params.limit) + 1,
                        //条件
                        orderNum: $("#orderNum").val().trim(),
                        recordType: $("#recordType").val().trim(),
                        startTime: $("#startTime").val().trim(),
                        endTime: $("#endTime").val().trim()
                    };
                },
                columns: [
                    {
                        title: '#', field: '', width: '50', align: 'left',
                        formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },
                    {field: 'tenantCode', title: '运营商代码', align: 'left', width: '100'},
                    {field: 'orderNum', title: '订单流水号', width: '120', align: 'left'},
                    {field: 'createdTime', title: '时间', width: '100', align: 'left'},
                    {field: 'recordTime', title: '入账时间', width: '100', align: 'left'},
                    {
                        field: 'recordType', title: '流水类型', width: '100', align: 'center',
                        formatter: function (value, row, index) {
                            if (value == '0') {
                                return '<span class="label label-success">充值</span>'
                            } else if (value == '1') {
                                return '<span class="label label-info">支出</span>'
                            }
                        }
                    },
                    {field: 'explains', title: '摘要', width: '100', align: 'center'},
                    {field: 'amount', title: '发生金额', width: '100', align: 'center'},
                    {
                        field: 'recordSource', title: '来源', width: '100', align: 'center',
                        formatter: function (value, row, index) {
                            if (value == '0') {
                                return '<span class="label label-success">平台</span>'
                            } else if (value == '1') {
                                return '<span class="label label-info">微信</span>'
                            }
                        }
                    },

                    {
                        title: '操作',
                        field: 'orderId',
                        width: '200', align: 'center',
                        formatter: function (value, row, index) {
                            var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" mce_href="#" title="录入入账时间" onclick="operationType.showModel(\''
                                + row.operatorAccountRecordId + '\')">录入入账时间</a> ';
                            var b = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" mce_href="#" title="详情" onclick="operationType.detailApply(\''
                                + row.operatorAccountRecordId + '\')">详情</a> ';
                            //生成内容
                            var operate = '';
                            if (null == row.recordTime) {
                                operate = operate + a;
                            }
                            operate = operate + b;
                            return operate;
                        }
                    }
                ]
            });
    };



    /*  模态框  */
    function showModel(operatorAccountRecordId) {
        $('#exampleModal').modal('show');

        $("#submitButton").unbind("click").bind("click", function () {
            $.ajax({
                url: prefix + "/updateRcOperatorAccountRecordTime",
                type: "post",
                data: {
                    "operatorAccountRecordId": operatorAccountRecordId,
                    "recordTime": $("#recordTime").val()
                },
                dataType: "json",
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').modal('hide')
                    }
                }
            })
        })

    }

    //详情
    function detailApply(applyId) {
        location.href = prefix + "/detail/" + applyId;
    }

    function exportRcOperatorAccountRecord() {
        $.fileDownload(prefix + "/exportRcOperatorAccountRecord", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                count: $("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text(),

                orderNum: $("#orderNum").val().trim(),
                recordType: $("#recordType").val().trim(),
                startTime: $("#startTime").val().trim(),
                endTime: $("#endTime").val().trim()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })

    }

    return {
        init: init,
        reLoad: reLoad,
        reset: reset,
        showModel: showModel,
        detailApply: detailApply,
        exportRcOperatorAccountRecord: exportRcOperatorAccountRecord,
    };
})();

